<?php

namespace App\Http\Controllers\Admin;
use App\Http\Model\UserModel;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;

require '/resources/org/code/Code.class.php';
class LoginController extends CommonController
{
    //
    public function Login(){
        if($input = Input::all()){
            if($input['user_name'] == '' || !isset($input['user_name'])){
                return back()->with('msg','未输入用户名');
            }
            if($input['user_pass'] == '' || !isset($input['user_pass'])){
                return back()->with('msg','未输入密码');
            }
            $codeClass = new \Code();
            if(strtolower($input['code']) != strtolower($codeClass->get())){
                echo $codeClass->get();
                return back()->with('msg','验证码错误');
            }
            $userModel = new  UserModel();
            $user = UserModel::where('user_name',$input['user_name'])->first();
            if(!$user){
                return back()->with('msg','无此用户');
            }
            if ($input['user_pass'] != Crypt::decrypt($user->user_pass)) {
                return back()->with('msg','密码错误');
            }
            session(['user'=>$user]);
            return redirect('admin/index');
        }
        return view('admin.login');
    }

    public function crypt(){
        $str = '123456';
        $p = 'eyJpdiI6InozUWlcL0dhc3FZVWQ3eE92ZFQ2MzNBPT0iLCJ2YWx1ZSI6IlFuV2VEYWhKNjJ2emxCVDVRdHFQUWc9PSIsIm1hYyI6ImJiODdlZDJhZjZiYmYzYzQ3ZGMxNTIxYjNhZTdlYzMzNjdjNDIyNTBlYTE0MjA1Yzc1NTAxZmI5NjUwZTM2OTgifQ';
        echo Crypt::decrypt($p);
        echo Crypt::encrypt($str);

    }

    public function check(){
        echo 'check';
    }

    public function code()
    {
        $code = new \Code();
        $code->make();
    }

    public function logout(){
        session(['user'=>null]);
        return redirect('admin/login');
    }

}
